<?
  class client {

   private $userID;

   public function login($loginname, $password)
   {
    if(empty($loginname) || empty($password))
     return 1;
     
    $loginname = Somm::Get('Tools')->checkStr($loginname);
    $password  = Somm::Get('Tools')->checkStr($password);
    
    if($loginname == '' && $password == '')
     return 2;

    $query = Somm::Get('mysql')->query('SELECT * FROM e_users WHERE name="'.$loginname.'" AND password="'.md5($password).'"');
    if($query->numRows() != 1)
     return 3;

    $data = $query->fetchArray();
    
    if($data['rank'] == 0)
     return 4;
     
    if($data['state'] > 0)
     return 5;

    $_SESSION['user'] = $data['ID'];
    $_SESSION['time'] = time();
    $_SESSION['rank'] = $data['rank'];
    $_SESSION['nickname'] = $data['name'];
     
    return 6;
   }
   
   public function auth()
   {
    if(!$_SESSION['user'] || $_SESSION['user'] == '')
     return 1;
     
    if((time() - $_SESSION['time']) > 300)
     return 2;

    $_SESSION['time'] = time();
     
    return 5;
   }
   
   public function logout()
   {
    if(!$_SESSION['user'] || $_SESSION['user'] == '')
     return 1;

    unset($_SESSION['user']);
    unset($_SESSION['time']);
    unset($_SESSION['nickname']);
    unset($_SESSION['rank']);
    
    return 3;
   }
  }